﻿using System;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.ViewEngines;
using TinyIoC;

namespace CodeflowsHost.Dashboard
{
    public class CustomNancyBootstrapper : DefaultNancyBootstrapper
    {
        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
        {
            base.ConfigureApplicationContainer(container);

            RegisterApplicationViewAssembly();
        }

        private void RegisterApplicationViewAssembly()
        {
            var viewsAssembly = GetType().Assembly;

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if(assembly != viewsAssembly)
                {
                    ResourceViewLocationProvider.Ignore.Add(assembly);

                    continue; 
                }

                ResourceViewLocationProvider.RootNamespaces.Add(viewsAssembly, "CodeflowsHost.Dashboard.Views");
            }
        }

        protected override NancyInternalConfiguration InternalConfiguration
        {
            get
            {
                return NancyInternalConfiguration.WithOverrides(
                    x => x.ViewLocationProvider = typeof (ResourceViewLocationProvider)
                    );
            }
        }
    }
}